package com.fr.report.web.ui;

import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.XMLFileManager;
import com.fr.base.XMLable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.util.ListMap;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.core.DataXMLUtils;
import com.fr.data.impl.NameWidget;
import com.fr.report.io.xml.SynchronizedVersion;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WidgetManager extends XMLFileManager
  implements XMLable
{
  public static final String XML_TAG = "WidgetManager";
  private ListMap nameWidgetConfigMap = new ListMap();
  private IconManager iconManager = null;

  public String fileName()
  {
    return "widgets.xml";
  }

  public Iterator getWidgetConfigNameIterator()
  {
    return this.nameWidgetConfigMap.keySet().iterator();
  }

  private WidgetConfig getStoredWidgetConfig(String paramString)
  {
    return ((WidgetConfig)this.nameWidgetConfigMap.get(paramString));
  }

  private void setIconManager(IconManager paramIconManager)
  {
    this.iconManager = paramIconManager;
  }

  public IconManager getIconManager()
  {
    if (this.iconManager == null)
      this.iconManager = new IconManager();
    return this.iconManager;
  }

  public WidgetConfig getWidgetConfig(String paramString)
  {
    WidgetConfig localWidgetConfig = getStoredWidgetConfig(paramString);
    try
    {
      return ((localWidgetConfig == null) ? null : (WidgetConfig)localWidgetConfig.clone());
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
    }
    return null;
  }

  public void putWidgetConfig(String paramString, WidgetConfig paramWidgetConfig)
  {
    if (paramWidgetConfig == null)
      this.nameWidgetConfigMap.remove(paramString);
    this.nameWidgetConfigMap.put(paramString, paramWidgetConfig);
  }

  public boolean renameWidgetConfig(String paramString1, String paramString2)
  {
    if ((!(this.nameWidgetConfigMap.containsKey(paramString1))) || (this.nameWidgetConfigMap.containsKey(paramString2)))
      return false;
    int i = this.nameWidgetConfigMap.indexOf(paramString1);
    this.nameWidgetConfigMap.put(i, paramString2, this.nameWidgetConfigMap.get(paramString1));
    return true;
  }

  public void removeWidgetConfig(String paramString)
  {
    this.nameWidgetConfigMap.remove(paramString);
  }

  public void clearAllWidgetConfig()
  {
    this.nameWidgetConfigMap.clear();
  }

  public static Widget widgetConfig2NameWidget(WidgetConfig paramWidgetConfig)
  {
    String str;
    WidgetManager localWidgetManager = FRContext.getWidgetManager();
    Iterator localIterator = localWidgetManager.getWidgetConfigNameIterator();
    do
    {
      if (!(localIterator.hasNext()))
        break label49;
      str = (String)localIterator.next();
    }
    while (!(ComparatorUtils.equals(localWidgetManager.getWidgetConfig(str), paramWidgetConfig)));
    return new NameWidget(str);
    label49: return null;
  }

  public static Widget widget2NameWidget(Widget paramWidget)
  {
    String str;
    WidgetManager localWidgetManager = FRContext.getWidgetManager();
    Iterator localIterator = localWidgetManager.getWidgetConfigNameIterator();
    do
    {
      if (!(localIterator.hasNext()))
        break label54;
      str = (String)localIterator.next();
    }
    while (!(ComparatorUtils.equals(localWidgetManager.getWidgetConfig(str).toWidget(), paramWidget)));
    return new NameWidget(str);
    label54: return paramWidget;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
      clearAllWidgetConfig();
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      String str2 = "";
      if (str1.equals("XMLVersion"))
      {
        if ((str2 = paramXMLableReader.getElementValue()) != null)
        {
          SynchronizedVersion localSynchronizedVersion = SynchronizedVersion.getSynchronizedVersion(Thread.currentThread());
          localSynchronizedVersion.setXmlVersionByString(str2);
        }
      }
      else if ("WidgetConfigMap".equals(str1))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final WidgetManager this$0;

          public void readXML()
          {
            if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("WidgetConfig")))
            {
              Object localObject = null;
              String str = null;
              if ((str = paramXMLableReader.getAttr("name")) != null)
                localObject = str;
              if (localObject == null)
                return;
              WidgetConfig localWidgetConfig = DataXMLUtils.readXMLWidgetConfig(paramXMLableReader);
              if (localWidgetConfig != null)
                this.this$0.putWidgetConfig(localObject, localWidgetConfig);
            }
          }
        });
      }
      else if ("IM".equals(str1))
      {
        this.iconManager = new IconManager();
        paramXMLableReader.readXMLObject(this.iconManager);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("WidgetManager");
    Iterator localIterator = getWidgetConfigNameIterator();
    if (localIterator.hasNext())
    {
      paramXMLPrintWriter.startTAG("WidgetConfigMap");
      while (true)
      {
        String str;
        WidgetConfig localWidgetConfig;
        while (true)
        {
          if (!(localIterator.hasNext()))
            break label72;
          str = (String)localIterator.next();
          localWidgetConfig = getStoredWidgetConfig(str);
          if (localWidgetConfig != null)
            break;
        }
        DataXMLUtils.writeXMLWidgetConfig(paramXMLPrintWriter, localWidgetConfig, str);
      }
      label72: paramXMLPrintWriter.end();
    }
    if (this.iconManager != null)
      this.iconManager.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return clone();
  }
}